package com.wzt.lianfirecontrol.adapter.xiaokongshi;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.user.UserInfoModel;
import com.wzt.lianfirecontrol.utils.StringUtils;

import java.util.List;

public class ChooseUserAdapter extends BaseRecyclerAdapter<UserInfoModel> {

    private Context context;

    private List<UserInfoModel> currentChooseList;

    public List<UserInfoModel> getCurrentChooseList() {
        return currentChooseList;
    }

    public void setCurrentChooseList(List<UserInfoModel> currentChooseList) {
        this.currentChooseList = currentChooseList;
        notifyDataSetChanged();
    }

    public ChooseUserAdapter(Context context, List<UserInfoModel> currentChooseList) {
        this.context = context;
        this.currentChooseList = currentChooseList;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_dialog_choose_user, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, UserInfoModel data) {
        if (viewHolder instanceof MyViewHolder){
            setMyData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item_content;
        private TextView tv_user_name;
        private TextView tv_user_phone;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initMyView(this, itemView);
        }
    }

    private void initMyView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_user_name = itemView.findViewById(R.id.tv_user_name);
        myViewHolder.tv_user_phone = itemView.findViewById(R.id.tv_user_phone);
    }


    private void setMyData(MyViewHolder viewHolder, UserInfoModel data, int realPosition) {
        viewHolder.tv_user_name.setText(data.getRealName());
        if (!StringUtils.isEmpty(data.getPhone())) {
            viewHolder.tv_user_phone.setText(data.getPhone());
        }else {
            viewHolder.tv_user_phone.setText("");
        }
        boolean isSelect = false;
        for (int i = 0; i < currentChooseList.size(); i++){
            if (data.getId().equals(currentChooseList.get(i).getId())){
                isSelect = true;
                break;
            }
        }

        if (isSelect){
            viewHolder.ll_item_content.setBackground(context.getResources().getDrawable(R.drawable.shape_main_color_tran_bg_frame));
        }else {
            viewHolder.ll_item_content.setBackgroundColor(context.getResources().getColor(R.color.white));
        }
    }

}
